(function () {
    'use strict';

    angular
        .module('app.category')
        .controller('categoryController', categoryController);

    categoryController.$inject = ['logger', '$state', '$rootScope', '$stateParams','PatientDemoData','$mdDialog','$timeout'];
    /* @ngInject */
    function categoryController(logger, $state, $rootScope, $stateParams, PatientDemoData, $mdDialog, $timeout) {
        var vm = this;
        
        vm.ShowPatDemo = ShowPatDemo;
        vm.ShowAttachments = ShowAttachments;
        vm.ShowPatDiagnosis = ShowPatDiagnosis;
        vm.ProcessReferral = ProcessReferral;
        vm.StatusDetail = StatusDetail;
        vm.DeleteReferral = DeleteReferral;

        activate();
        $timeout(function () {
            $mdDialog.hide();
        }, 5000);
        

        function ShowPatDemo(ReferralID) {
            
            $state.go('patientdemo', { ReferralID: ReferralID })
        }

        function ShowPatDiagnosis(ReferralID) {
            
            $state.go('diagnosis', { ReferralID: ReferralID })
        }

        function ShowAttachments(ReferralID) {
            $state.go('attachments', { ReferralID: ReferralID })
        }

        function ProcessReferral(ReferralID) {
            $state.go('processreferral', { ReferralID: ReferralID })
        }



        function StatusDetail(ReferralID) {
            $rootScope.LogReferralID = ReferralID;
            $mdDialog.show({
                controller: 'ReferralStatusController',
                templateUrl: 'app/ReferralStatus/ReferralStatus.html',
                parent: angular.element(document.body),
                controllerAs: 'vm',
                clickOutsideToClose: false,
                fullscreen: false
            }).then(function (answer) {
            }, function () {

            });
        };

        function activate() {
            try {
                showWait();

                if ($stateParams.Type == 'Completed' || $stateParams.Type == 'Referral-Out') {
                    vm.DisableProcess = true;
                }

                PatientDemoData.GetPatientForCategory($stateParams.Type, $stateParams.Category)
                .then(function (response) {
                    $mdDialog.hide();
                    vm.PatientData = response.data.ReferralList;
                    $mdDialog.hide();
                });

                $rootScope.ShowCrumbs = true;
                $rootScope.ShowPracticeCrumb = true;
            }
            catch(ex)
            {
                alert('error');
            }
            finally {
                $mdDialog.hide();
            }
            
            //logger.info('Activated category View');
        }

        function showWait() {
            $mdDialog.show({
                controller: 'LoginController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        }

        function DeleteReferral(ReferralID) {

           var confirm = $mdDialog.confirm()
         .title('Would you like to delete the referral?')
         .ariaLabel('Delete Referral')
         .ok('Yes, Delete.')
         .cancel('No, Cancel.');
           $mdDialog.show(confirm).then(function () {
               showWait();
                PatientDemoData.DeleteReferrral(ReferralID)
               .then(function (response) {
                   if (parseInt(response.data) > 0) {
                       $timeout(function () {
                           $mdDialog.show(
                               $mdDialog.alert()
                               .parent(angular.element(document.body))
                               .clickOutsideToClose(true)
                               .title('Delete Status')
                               .textContent('Referral was deleted.')
                               .ariaLabel('Delete Status Dialog')
                               .ok('Close')
                           ).then(function (response) {
                               $timeout(function () {
                                   showWait();
                                   PatientDemoData.GetPatientForCategory($stateParams.Type, $stateParams.Category)
                                   .then(function (response) {
                                       $mdDialog.hide();
                                       vm.PatientData = response.data.ReferralList;
                                       $mdDialog.hide();
                                       if (!vm.PatientData) {
                                           $timeout(function () {
                                               $state.go('dashboard');
                                           }, 100);
                                       }
                                   });
                               }, 100);
                           });
                       }, 2000);
                   }
               });
            }, function () {
                $mdDialog.hide();
                $mdDialog.hide();
            });


            
        }
    }
})();


